package com.hgkj.cloud.base.file;

import java.io.File;
import java.io.IOException;


public class FileSourceGenerator {
	//本地目录
	private String clientDir;
	//图片服务器的访问目录 如果"/"开头表示在同一台服务器上,如果以http开头表示在不同的机器
	private String serverPath;
	public String getClientDir() {
		return clientDir;
	}
	public void setClientDir(String clientDir) {
		this.clientDir = clientDir;
	}
	public String getServerPath() {
		return serverPath;
	}
	public void setServerPath(String serverPath) {
		this.serverPath = serverPath;
	}
	
	public String generator(FileSaver fs) throws IOException {
		String fileExt = fs.getExtension();
		if(!fileExt.startsWith(".")) fileExt = "." + fileExt;
		
		File tmpFile = File.createTempFile("cloud", fileExt, new File(clientDir));
		tmpFile.deleteOnExit();
		fs.saveFile(tmpFile);
		fs.setLocalFile(tmpFile);
		return serverPath+tmpFile.getName();
		
	}
	
	public String generatorOpen(FileSaver fs) throws IOException {
		String fileExt = fs.getExtensionOpen();
		if(!fileExt.startsWith(".")) fileExt = "." + fileExt;
		
		File tmpFile = File.createTempFile("cloud", fileExt, new File(clientDir));
		tmpFile.deleteOnExit();
		fs.saveFileOpen(tmpFile);
		
		return serverPath+tmpFile.getName();
		
	}
	
}
